package com.live.shop.view;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.live.shop.R;

public class ToastUtils {
	public static void Setinfo(Context context,String info,int i){
		//自定义Toast控件
		View toastView = LayoutInflater.from(context).inflate(R.layout.toast_layout, null);
		LinearLayout relativeLayout = toastView.findViewById(R.id.toast_linear);
		//动态设置toast控件的宽高度，宽高分别是130dp
		//这里用了一个将dp转换为px的工具类PxUtil
		RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams((int) dpToPx(context, 150), (int)dpToPx(context, 150));
		relativeLayout.setLayoutParams(layoutParams);
		TextView text = toastView.findViewById(R.id.toast_text);
		ImageView image = toastView.findViewById(R.id.toast_image);
		switch (i) {
			case 1:
				image.setBackgroundResource(R.mipmap.toast_ok_icon);
				break;
			case 2:
				image.setBackgroundResource(R.mipmap.toast_no_icon);
				break;
		}
		text.setText(info);
		Toast toast = new Toast(context);
		toast.setDuration(Toast.LENGTH_SHORT);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.setView(toastView);
		toast.show();
	}
	public static float dpToPx(Context context, int dp) {
		//获取屏蔽的像素密度系数
		float density = context.getResources().getDisplayMetrics().density;
		return dp * density;
	}
	public static float pxTodp(Context context, int px) {
		//获取屏蔽的像素密度系数
		float density = context.getResources().getDisplayMetrics().density;
		return px / density;
	}
}
